package org.daidai.abstrusewriting.homepage.top.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.top.images.CircleTransform;
import org.daidai.abstrusewriting.homepage.top.model.RankingListDetail;

import java.util.List;

/**
 * Created by liuch on 2016/10/31.
 */

public class ListItemDetailAdapter extends BaseAdapter {

    private Context context;
    private List<RankingListDetail.ListItem> list;

    public ListItemDetailAdapter(Context context, List<RankingListDetail.ListItem> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(context).inflate(R.layout.list_item , parent , false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null){
            holder = new ViewHolder(ret);
            ret.setTag(holder);
        }

        holder.bindView(list.get(position));
        holder.txtNumber.setText(Integer.toString(position + 1));

        return ret;
    }

    public static class ViewHolder{
        private final TextView txtNumber;
        private final ImageView imageView;
        private final TextView txtTitle;
        private final TextView txtAuthor;

        public ViewHolder(View item) {
            txtNumber = ((TextView) item.findViewById(R.id.list_item_number));
            imageView = ((ImageView) item.findViewById(R.id.list_item_image));
            txtTitle = ((TextView) item.findViewById(R.id.list_item_title));
            txtAuthor = ((TextView) item.findViewById(R.id.list_item_author));
        }

        public void bindView(RankingListDetail.ListItem item){
            txtAuthor.setText(item.getNickname());
            txtTitle.setText(item.getTitle());
            String coverSmall = item.getCoverSmall();
            if (coverSmall != null && !"".equals(coverSmall)) {
                Picasso.with(imageView.getContext()).load(coverSmall).into(imageView);
//                Picasso.with(imageView.getContext()).load(coverSmall).transform(new CircleTransform()).into(imageView);
            }
        }
    }
}
